import { expandAndToString } from '@/utils/common';
import { FilterGroup } from '#/request';
import { AdColumn } from '@/components/AdVxeTable';
import { TreeItem } from '@/components/Tree/index';

import { AdChooseList, AdChooseListDialog, useAdChooseList } from '@/components/AdChooseList';
import { readUsers } from '@/api/identity/admin/user';

import { getAccessTypeEnumSchema } from '@/enums/accessTypeEnum';
import { getGenderEnumSchema } from '@/enums/genderEnum';
import { getChineseZodiacSignEnumSchema } from '@/enums/chineseZodiacSignEnum';
import { getStarSignEnumSchema } from '@/enums/starSignEnum';
import { useI18n } from '@/hooks/web/useI18n';

const { t } = useI18n();
const genderEnumSchema = getGenderEnumSchema();
const zodiacSignEnumSchema = getChineseZodiacSignEnumSchema();
const starSignEnumSchema = getStarSignEnumSchema();
const accessTypeEnumSchema = getAccessTypeEnumSchema();

export const columns: AdColumn[] = [
  {
    title: t('entity.idTitle'),
    field: 'Id',
    width: 180,
    visible: false,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'integer',
  },
  {
    title: t('routes.system.identity.user.userNameTitle'),
    field: 'UserName',
    width: 150,
    formVisible: true,
    formEditable: true,
    required: true,
    filterable: true,
    dataType: 'string',
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.nickNameTitle'),
    field: 'NickName',
    width: 200,
    formVisible: true,
    formEditable: true,
    required: true,
    filterable: true,
    dataType: 'string',
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.passwordTitle'),
    field: 'Password',
    width: 0,
    visible: false,
    formVisible: true,
    formEditable: true,
    filterable: false,
    dataType: 'password',
  },
  {
    title: t('routes.system.identity.user.emailTitle'),
    field: 'Email',
    width: 180,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'email',
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.emailConfirmedTitle'),
    field: 'EmailConfirmed',
    width: 98,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'boolean',
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.phoneNumberTitle'),
    field: 'PhoneNumber',
    width: 160,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'mobile',
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.phoneConfirmedTitle'),
    field: 'PhoneConfirmed',
    width: 98,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'boolean',
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.genderTitle'),
    field: 'Gender',
    width: 70,
    formVisible: true,
    formEditable: true,
    required: true,
    filterable: true,
    filterIndex: 'Detail.Gender',
    dataType: 'enum',
    enumData: genderEnumSchema,
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.birthdayTitle'),
    field: 'Birthday',
    width: 120,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.Birthday',
    dataType: 'date',
  },
  {
    title: t('routes.system.identity.user.bloodTypeTitle'),
    field: 'BloodType',
    width: 100,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.BloodType',
    dataType: 'string',
  },
  {
    title: t('routes.system.identity.user.tagTitle'),
    field: 'Tag',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterIndex: 'Detail.Tag',
    dataType: 'array',
  },
  {
    title: t('routes.system.identity.user.userTitleTitle'),
    field: 'Title',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterIndex: 'Detail.Title',
    dataType: 'array',
  },
  {
    title: t('routes.system.identity.user.addressTitle'),
    field: 'Address',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.Address',
    dataType: 'text',
  },
  {
    title: t('routes.system.identity.user.profileTitle'),
    field: 'Profile',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.Profile',
    dataType: 'text',
  },
  {
    title: t('routes.system.identity.user.signatureTitle'),
    field: 'Sign',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.Sign',
    dataType: 'text',
  },
  {
    title: t('routes.system.identity.user.chineseBirthdayTitle'),
    field: 'ChineseBirthday',
    width: 120,
    formVisible: true,
    formEditable: false,
    filterable: false,
    dataType: 'date',
  },
  {
    title: t('routes.system.identity.user.ageTitle'),
    field: 'Age',
    width: 50,
    formVisible: true,
    formEditable: false,
    filterable: false,
    dataType: 'integer',
  },
  {
    title: t('routes.system.identity.user.zodiacSignTitle'),
    field: 'ZodiacSign',
    width: 50,
    formVisible: false,
    filterable: false,
    dataType: 'enum',
    enumData: zodiacSignEnumSchema,
  },
  {
    title: t('routes.system.identity.user.starSignTitle'),
    field: 'StarSign',
    width: 75,
    formVisible: false,
    filterable: false,
    dataType: 'enum',
    enumData: starSignEnumSchema,
  },
  {
    title: t('routes.system.identity.user.remarkTitle'),
    field: 'Remark',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.Remark',
    dataType: 'text',
  },
  {
    title: t('routes.system.identity.user.homePathTitle'),
    field: 'HomePath',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.HomePath',
    dataType: 'text',
  },
  {
    title: t('routes.system.identity.user.rolesTitle'),
    field: 'Roles',
    width: 200,
    formVisible: false,
    formEditable: false,
    filterable: false,
    dataType: 'array',
    formatter: ({ row }) => expandAndToString(row.Roles),
  },
  {
    title: t('entity.lockable.isLockedTitle'),
    field: 'IsLocked',
    width: 75,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'boolean',
  },
  {
    title: t('routes.system.identity.user.lockoutEnabledTitle'),
    field: 'LockoutEnabled',
    width: 90,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'boolean',
  },
  {
    title: t('routes.system.identity.user.accessFailedCountTitle'),
    field: 'AccessFailedCount',
    width: 110,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'integer',
  },
  {
    title: t('routes.system.identity.user.lockoutEndTitle'),
    field: 'LockoutEnd',
    width: 180,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'datetime',
  },
  {
    title: t('entity.creationAudited.createdTimeTitle'),
    field: 'CreatedTime',
    width: 180,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'datetime',
  },
];

export const userFunctionColumns: AdColumn[] = [
  {
    title: t('routes.system.authorization.functionIdTitle'),
    field: 'Id',
    width: 0,
    visible: false,
    filterable: false,
    dataType: 'integer',
  },
  {
    title: t('routes.system.authorization.functionNameTitle'),
    field: 'Name',
    width: 300,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('routes.system.authorization.functionAccessTypeTitle'),
    field: 'AccessType',
    width: 100,
    filterable: true,
    dataType: 'enum',
    enumData: accessTypeEnumSchema,
  },
  {
    title: t('routes.system.authorization.functionAreaTitle'),
    field: 'Area',
    width: 150,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('routes.system.authorization.functionControllerTitle'),
    field: 'Controller',
    width: 200,
    filterable: true,
    dataType: 'string',
  },
];

/** 树形数据项 */
export interface TreeDataItem extends TreeItem {
  description: string;
  children?: TreeDataItem[];
}
/**
 * 将原始树数据节点转换为 `TreeItem`节点
 * @param nodes 原始树数据节点
 * @param checkedKeys 选中值
 */
export function makeUserModulesTree(nodes: any[], checkedKeys: string[]): TreeDataItem[] {
  if (!nodes.length) {
    return [];
  }
  const result: TreeDataItem[] = [];
  for (const node of nodes) {
    const icon = node.HasChildren
      ? 'fluent:text-bullet-list-tree-24-filled'
      : node.HasAuth
        ? 'emojione-v1:white-heavy-check-mark'
        : 'noto-v1:check-mark-button';
    // const hasAuth = node.HasChildren ? false : node.HasAuth;
    // const title = `${node.FullName.replace('根节点-', '')}${hasAuth ? '(已授权)' : ''} --> ${
    //   node.Code
    // }`;
    let title = node.FullName;
    const fullNameArr = node.FullName.split('-');
    if (fullNameArr?.length > 1) {
      title = node.FullName.replace(`${fullNameArr[0]}-`, '');
    }
    const item: TreeDataItem = {
      icon: icon,
      key: node.Id as string,
      title: title,
      checked: node.IsChecked as boolean,
      description: node.Name as string,
      isLeaf: !node.HasChildren,
    };
    if (node.IsChecked) {
      checkedKeys.push(node.Id);
    }
    if (node.Children && node.Children.length > 0) {
      item.children = makeUserModulesTree(node.Children, checkedKeys);
    }
    result.push(item);
  }
  return result;
}

export const chooseColumns: AdColumn[] = [
  {
    title: t('entity.idTitle'),
    field: 'Id',
    width: 180,
    visible: false,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'integer',
  },
  {
    title: t('routes.system.identity.user.userNameTitle'),
    field: 'UserName',
    width: 150,
    formVisible: true,
    formEditable: true,
    required: true,
    filterable: true,
    dataType: 'string',
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.nickNameTitle'),
    field: 'NickName',
    width: 200,
    formVisible: true,
    formEditable: true,
    required: true,
    filterable: true,
    dataType: 'string',
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.emailTitle'),
    field: 'Email',
    width: 180,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'email',
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.phoneNumberTitle'),
    field: 'PhoneNumber',
    width: 160,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'mobile',
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.genderTitle'),
    field: 'Gender',
    width: 70,
    formVisible: true,
    formEditable: true,
    required: true,
    filterable: true,
    filterIndex: 'Detail.Gender',
    dataType: 'enum',
    enumData: genderEnumSchema,
    sortable: true,
  },
  {
    title: t('routes.system.identity.user.birthdayTitle'),
    field: 'Birthday',
    width: 120,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.Birthday',
    dataType: 'date',
  },
  {
    title: t('routes.system.identity.user.bloodTypeTitle'),
    field: 'BloodType',
    width: 100,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.BloodType',
    dataType: 'string',
  },
  {
    title: t('routes.system.identity.user.tagTitle'),
    field: 'Tag',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterIndex: 'Detail.Tag',
    dataType: 'array',
  },
  {
    title: t('routes.system.identity.user.userTitleTitle'),
    field: 'Title',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterIndex: 'Detail.Title',
    dataType: 'array',
  },
  {
    title: t('routes.system.identity.user.addressTitle'),
    field: 'Address',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.Address',
    dataType: 'text',
  },
  {
    title: t('routes.system.identity.user.profileTitle'),
    field: 'Profile',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.Profile',
    dataType: 'text',
  },
  {
    title: t('routes.system.identity.user.signatureTitle'),
    field: 'Sign',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.Sign',
    dataType: 'text',
  },
  {
    title: t('routes.system.identity.user.chineseBirthdayTitle'),
    field: 'ChineseBirthday',
    width: 120,
    formVisible: true,
    formEditable: false,
    filterable: false,
    dataType: 'date',
  },
  {
    title: t('routes.system.identity.user.ageTitle'),
    field: 'Age',
    width: 50,
    formVisible: true,
    formEditable: false,
    filterable: false,
    dataType: 'integer',
  },
  {
    title: t('routes.system.identity.user.zodiacSignTitle'),
    field: 'ZodiacSign',
    width: 50,
    formVisible: false,
    filterable: false,
    dataType: 'enum',
    enumData: zodiacSignEnumSchema,
  },
  {
    title: t('routes.system.identity.user.starSignTitle'),
    field: 'StarSign',
    width: 75,
    formVisible: false,
    filterable: false,
    dataType: 'enum',
    enumData: starSignEnumSchema,
  },
  {
    title: t('routes.system.identity.user.remarkTitle'),
    field: 'Remark',
    width: 200,
    formVisible: true,
    formEditable: true,
    filterable: true,
    filterIndex: 'Detail.Remark',
    dataType: 'text',
  },
  {
    title: t('entity.lockable.isLockedTitle'),
    field: 'IsLocked',
    width: 75,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'boolean',
  },
  {
    title: t('entity.creationAudited.createdTimeTitle'),
    field: 'CreatedTime',
    width: 180,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'datetime',
  },
];

/**
 * 高级选择列表
 * @param multiple 列表多选
 * @param searchInfo 额外的请求参数
 * @param beforeFetch 请求之前处理参数
 */
export function useChooseList(
  multiple: boolean | (() => boolean) = false,
  searchInfo?: FilterGroup,
  beforeFetch?: Fn,
) {
  const { dialogRegister, getDialogProps, getProps } = useAdChooseList({
    api: readUsers,
    columns: chooseColumns,
    dialogId: 'ea_identity_user_choose_list',
    dialogTitle: t('routes.system.identity.user.userList'),
    dialogMultiple: multiple,
    searchInfo: searchInfo,
    beforeFetch: beforeFetch,
  });

  return { AdChooseList, AdChooseListDialog, dialogRegister, getDialogProps, getProps };
}
